package cn.com.xinjinfu.pdoulcProduct.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;

import cn.com.xinjinfu.pdoulcProduct.R;
import cn.com.xinjinfu.pdoulcProduct.activity.FixedProduct.ActivitMoneyAct;
import cn.com.xinjinfu.pdoulcProduct.activity.user.ActCertification;
import cn.com.xinjinfu.pdoulcProduct.engine.DataHandler;
import cn.com.xinjinfu.pdoulcProduct.entity.User;
import cn.com.xinjinfu.pdoulcProduct.manager.AccountManager;
import cn.com.xinjinfu.pdoulcProduct.manager.ActivityManager;
import cn.com.xinjinfu.pdoulcProduct.manager.L;
import cn.com.xinjinfu.pdoulcProduct.manager.TitleManager;
import cn.com.xinjinfu.pdoulcProduct.manager.ToastManager;
import cn.com.xinjinfu.pdoulcProduct.view.LoadStatusBox;
import cn.com.xinjinfu.pdoulcProduct.view.ProgressWebView;


/**
 * @Description:WebView界面，带自定义进度条显示
 * @author http://blog.csdn.net/finddreams
 */ 
public class DoWorkActivity extends Activity {
	protected ProgressWebView mWebView;
	private final String ACTION_NAME = "跳转首页广播";
	private LoadStatusBox loadbox;
	private LinearLayout top_right_ll;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_baseweb);
		TitleManager.showTitle(this, "我的P兜任务", "活动本金");
		ActivityManager.getInstance().addActivity(this);
		top_right_ll = (LinearLayout) findViewById(R.id.top_right_ll);
		top_right_ll.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				startActivity(new Intent(DoWorkActivity.this,ActivitMoneyAct.class));
			}
		});
		mWebView = (ProgressWebView) findViewById(R.id.baseweb_webview);
		mWebView.getSettings().setJavaScriptEnabled(true);
		loadbox = (LoadStatusBox) this.findViewById(R.id.loadStatusBox);
		loadbox.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				netCheck();
			}
		});
		loadbox.loading();
		netCheck();
	}

	private void netCheck(){
		if (!DataHandler.isNetworkConnected(this)) {
			loadbox.failed();
			ToastManager.show(this, "请检查网络连接");
			return;
		}else {
			initData();
			loadbox.success();
		}
	}

	protected void initData() {
		User user = AccountManager.getInstance().getUser();
		String url = DataHandler.WEBURL + "/front/wa/appMyTask?id="+ user.getCurrUser().getSign();
		L.d("url======================="+url);
		mWebView.loadUrl(url);
		// 设置Web视图
		mWebView.setWebViewClient(new MyWebViewClient());
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		ActivityManager.getInstance().exitActivity();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		mWebView = null;

	}
	// 监听 所有点击的链接，如果拦截到我们需要的，就跳转到相对应的页面。
	private class MyWebViewClient extends WebViewClient {
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
//			System.out.println("-------shouldOverrideUrlLoading 。。 url:" + url);
			if (url != null) {
				if (url.contains("/front/wa/realName")) {//实名
					Intent intent = new Intent(DoWorkActivity.this, ActCertification.class);
					intent.putExtra("id", "");
					intent.putExtra("mobile","");
					intent.putExtra("pwd","");
					intent.putExtra("cer_style_falg",1);
					startActivity(intent);
					return true;
				}else if (url.contains("/front/wa/transferItems")) {//转让列表
					Intent mIntent = new Intent(ACTION_NAME);
					mIntent.putExtra("yaner","transfer");
					//发送广播
					sendBroadcast(mIntent);
					ActivityManager.getInstance().exitActivity();
					return true;
				}else if (url.contains("/front/wa/bidsItems")) {//定期
					Intent mIntent = new Intent(ACTION_NAME);
					mIntent.putExtra("yaner","fixed");
					sendBroadcast(mIntent);
					ActivityManager.getInstance().exitActivity();
					return true;
				}else if (url.contains("/front/wa/signInBasic")) {//签到
					Intent mIntent = new Intent(ACTION_NAME);
					mIntent.putExtra("yaner", "fixed");
					sendBroadcast(mIntent);
					ActivityManager.getInstance().exitActivity();
					return true;
				}
			}
			return super.shouldOverrideUrlLoading(view, url);
		}


		@Override
		public void onPageFinished(WebView view, String url) {
			view.getSettings().setJavaScriptEnabled(true);
			super.onPageFinished(view, url);
		}
	}

}
